package controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.swing.JOptionPane;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import domain.Address;
import domain.Contact;
import domain.ContactGroup;
import domain.DAOContact;
import domain.PhoneNumber;

public class AddContactController {
	private Contact contact = new Contact();
	private String mobileNumber;
	private String homeNumber;
	private String workNumber;
	private DAOContact dao;
	private String siret;

	public boolean isFamilleCheckBox() {
		return familleCheckBox;
	}

	public void setFamilleCheckBox(boolean familleCheckBox) {
		this.familleCheckBox = familleCheckBox;
	}

	public boolean isAmisCheckBox() {
		return amisCheckBox;
	}

	public void setAmisCheckBox(boolean amisCheckBox) {
		this.amisCheckBox = amisCheckBox;
	}

	public boolean isTravailCheckBox() {
		return travailCheckBox;
	}

	public void setTravailCheckBox(boolean travailCheckBox) {
		this.travailCheckBox = travailCheckBox;
	}

	private boolean familleCheckBox;
	private boolean amisCheckBox;
	private boolean travailCheckBox;

	public String getSiret() {
		return siret;
	}

	public void setSiret(String siret) {
		this.siret = siret;
	}

	public DAOContact getDao() {
		return dao;
	}

	public void setDao(DAOContact dao) {
		this.dao = dao;
	}

	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public String getMobileNumber() {
		return mobileNumber;
	}

	public void setMobileNumber(String mobileNumber) {
		this.mobileNumber = mobileNumber;
	}

	public String getHomeNumber() {
		return homeNumber;
	}

	public void setHomeNumber(String homeNumber) {
		this.homeNumber = homeNumber;
	}

	public String getWorkNumber() {
		return workNumber;
	}

	public void setWorkNumber(String workNumber) {
		this.workNumber = workNumber;
	}

	public String saveContact() {
		try {
			ApplicationContext context = WebApplicationContextUtils
					.getWebApplicationContext((ServletContext) FacesContext
							.getCurrentInstance().getExternalContext()
							.getContext());
			DAOContact dao = (DAOContact) context.getBean("DaoContactBean");
			HashMap<String, String> parameter = new HashMap<String, String>();
			ArrayList<String> checkedBoxes = new ArrayList<String>();
			populateCheckbox(checkedBoxes);
			parameter.put("firstName", contact.getFirstName());
			parameter.put("lastName", contact.getLastName());
			parameter.put("email", contact.getEmail());
			parameter.put("street", contact.getAddress().getStreet());
			parameter.put("city", contact.getAddress().getCity());
			parameter.put("zip", contact.getAddress().getZip());
			parameter.put("country", contact.getAddress().getCountry());
			parameter.put("mobilephone", mobileNumber);
			parameter.put("homephone", homeNumber);
			parameter.put("officenumber", workNumber);
			parameter.put("siret", siret);
			contact.setFirstName("");
			contact.setLastName("");
			contact.setEmail("");
			contact.setAddress(new Address());
			contact.setBooks(new HashSet<ContactGroup>());
			contact.setProfiles(new HashSet<PhoneNumber>());
			return dao.addContact(parameter, checkedBoxes);
		} catch (Exception e) {
			return " Erreur lors de l'ajout du contact";
		}
	}
	
	public void populateCheckbox(ArrayList<String> checkedBoxes){
		if (amisCheckBox) {
			checkedBoxes.add("amis");
		}
		if (familleCheckBox) {
			checkedBoxes.add("famille");
		}
		if (travailCheckBox) {
			checkedBoxes.add("travail");
		}
	}
}
